/******************************************************************************\
*  Animeshell 1.0 Multimedia manager                                           *
*  Copyright (C) 2006-2007 Rémy Guillemette <shaikadzari@gmail.com>            *
*                                                                              *
*  This program is free software; you can redistribute it and/or modify        *
*  it under the terms of the GNU General Public License version 2 only as      *
*  published by the Free Software Foundation.                                  *
*                                                                              *
*  This program is distributed in the hope that it will be useful,             *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
*  GNU General Public License for more details.                                *
*                                                                              *
*  You should have received a copy of the GNU General Public License           *
*  along with this program; if not, write to the                               *
*  Free Software Foundation, Inc.,                                             *
*  59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                   *
\******************************************************************************/

#include <QDir>
#include <QTextStream>
#include <QFile>
#include "animeshellrc.h"


#define PROFIL "anime"
#define FOLDER "/.animeshell"
#define FILTER "Anime"
#define BROWSER "konqueror"
#define WEBSEARCH "http://anidb.info"
#define EDITOR "gimp"
#define PLAYER "gmplayer"
#define CONFIG "/.animeshellrc"



animeshellrc::animeshellrc(QString pProf1, QString pProf2, QString pProf3)
{
	
	// Get the /home directory
    QString Config = QDir::homePath();
    QDir AppFolder;
    bool rcExist = true;
    
    // Add CONFIG to the path
    Config += CONFIG;

    // 
    if ( !QFile::exists( Config ) )
     	rcExist = false;
     	
    m_Settings = new QSettings(Config, QSettings::IniFormat);
     
    if ( !rcExist ) // Must create the profile
    {
     	//
     	
     	m_Settings->beginGroup(pProf1);
     	
   		m_Settings->setValue("folder", QDir::homePath() + FOLDER);
	    m_Settings->setValue("filter", FILTER);
	    m_Settings->setValue("browser", BROWSER);
	    m_Settings->setValue("websearch", WEBSEARCH);
	    m_Settings->setValue("editor", EDITOR);
	    m_Settings->setValue("player", PLAYER);
	    m_Settings->endGroup();
   		//
   		
	    m_Settings->beginGroup(pProf2);
   		
   		m_Settings->setValue("folder", QDir::homePath() + FOLDER);
   		m_Settings->setValue("filter", QObject::tr("Film"));
   		m_Settings->setValue("browser", BROWSER);
   		m_Settings->setValue("websearch", "www.imdb.com");
   		m_Settings->setValue("editor", EDITOR);
   		m_Settings->setValue("player", PLAYER);
   		m_Settings->endGroup();
   		//
   		
   		m_Settings->beginGroup(pProf3);
   		
   		m_Settings->setValue("folder", QDir::homePath() + FOLDER);
   		m_Settings->setValue("filter", QObject::tr("Musique"));
   		m_Settings->setValue("browser", BROWSER);
   		m_Settings->setValue("websearch", "www.google.ca");
   		m_Settings->setValue("editor", EDITOR);
   		m_Settings->setValue("player", "amarokapp");
   		m_Settings->endGroup();
    }
     
   	// List all groups
    m_ListProfile = m_Settings->childGroups();
    m_CurrentProfile = 0;
    
    // Create default ~/.animeshell folder
    if ( !AppFolder.exists( QDir::homePath() + FOLDER ) )
    {
    	if ( AppFolder.mkdir(QDir::homePath() + FOLDER) )
    	{
    		AppFolder.mkdir( QDir::homePath() + FOLDER + "/images" );
   		}
   	}
    	
}

animeshellrc::~animeshellrc()
{
	
}

bool animeshellrc::CreateNewFolder(QString pFolder, QString pPath, bool pNewBd)
{
	// Variables
	QDir AppDir(pPath);
	QString CompPath(pPath + pFolder);
	QString currentfolder = GetQSettingsChild(m_ListProfile[m_CurrentProfile], "folder");
	
	if ( AppDir.exists() )
	{
		if ( AppDir.mkdir(pFolder) )
		{
			// Copy the database
			if ( pNewBd )
				QFile::copy( currentfolder + "/AnimeshellSQLite-new", CompPath + "/AnimeshellSQLite" );
			else
				QFile::copy( currentfolder + "/AnimeshellSQLite", CompPath + "/AnimeshellSQLite" );	
		}
	}
	
	return true;	
}

QStringList animeshellrc::GetQSettings(QString pName)
{
	QStringList mylist;
	QString ProfileName;
	
	if ( pName.isNull() )
		ProfileName = m_ListProfile[m_CurrentProfile];
	else
		ProfileName = pName;
		
	
	if ( m_ListProfile.contains(ProfileName, Qt::CaseSensitive) )
	{
		m_Settings->beginGroup(ProfileName);
	    mylist << m_Settings->value("folder", FOLDER).toString();
	    mylist << m_Settings->value("filter", FILTER).toString();
	    mylist << m_Settings->value("browser", BROWSER).toString();
	    mylist << m_Settings->value("websearch", WEBSEARCH).toString();
	    mylist << m_Settings->value("editor", EDITOR).toString();
	    mylist << m_Settings->value("player", PLAYER).toString();
	    m_Settings->endGroup();
	}
	
	return mylist;
	
}
		
		
QString animeshellrc::GetQSettingsChild(QString pName, QString pChildName)
{
	QString ret;
	
	if ( m_ListProfile.contains(pName, Qt::CaseSensitive) )
	{
		m_Settings->beginGroup(pName);
	    ret = m_Settings->value(pChildName).toString();
	    m_Settings->endGroup();
    }
    
    return ret;	
}

void animeshellrc::SaveAllProfileOnClose()
{
	delete m_Settings;
}

QStringList animeshellrc::getListProfile()
{
	return m_ListProfile;
}

//
